package com.example.smartplay.controller;


import com.example.smartplay.entity.Admin;
import com.example.smartplay.entity.Result;
import com.example.smartplay.entity.Student;
import com.example.smartplay.mapper.AdminMapper;
import com.example.smartplay.mapper.StudentMapper;
import com.example.smartplay.service.StudentService;
import com.example.smartplay.utils.Md5Util;
import com.example.smartplay.utils.ThreadLocalUtil;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-07-11
 */
@RestController
@RequestMapping("/user")
public class UserController {
    //注册
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentService studentService;
    @Autowired
    private AdminMapper adminMapper;


    @PostMapping("/register")
    public Result register(String username, String password) {


        // 查询用户是否存在
        Student student = studentService.findByUsername(username);
        if (student != null) {
            return Result.error("用户已存在");
        }

        // 注册用户
        studentService.register(username, password);
        return Result.success();
    }
        //根据用户名获取用户信息
    @GetMapping("/userInfo")
    public Result userinfo(){
        Map<String, Object> userinfo = ThreadLocalUtil.get();
        String username = (String) userinfo.get("username");
        Student student = studentService.findByUsername(username);
        if (student != null) { return Result.success(student);}
        Admin admin = adminMapper.findByUsername(username);
        return Result.success(admin);


    }
    //修改密码
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody Map<String, String> passwordData)  {
        String oldPassword = passwordData.get("oldPassword");
        String newPassword = passwordData.get("newPassword");
        String newPassword2 = passwordData.get("newPassword2");

        if (newPassword == null || newPassword2 == null || !newPassword.equals(newPassword2)) {
            return Result.error("两次输入的密码不一致或密码为空");
        }
        //如果旧密码和新密码相同
        if (oldPassword.equals(newPassword)) {
            return Result.error("新密码不能与旧密码相同");
        }
        //解析token 并获取到studnetid
       /*  Map<String, Object> claims = JwtUtil.parseToken(tokenHeader);
        Integer studentId = (Integer) claims.get("id");*/


        Map<String, Object> map = ThreadLocalUtil.get();
        Integer studentId = (Integer) map.get("id");


        Student student = studentMapper.selectById(studentId);
        if (student == null) {
            return Result.error("找不到对应的学生信息");
        }

        // 验证旧密码
        if (Md5Util.getMD5String(oldPassword).equals(student.getPassword())) {
            // 更新密码
            student.setPassword(Md5Util.getMD5String(newPassword));
            studentService.updateById(student);
            return Result.success();
        } else {
            return Result.error("旧密码错误");
        }
    }
}
